if (!window["vg"]) {
    vg = {};
}

if (!vg.utility) {
    vg.utility = {};
}

vg.utility.Report = (function() {
    var mapXmlPrefix = "xmlLFButtonMap";
    var appXmlPrefix = "xmlLFButtonApplicationDefinition";
    var reportNamespaces = {
        'SOAP': 'http://schemas.xmlsoap.org/soap/envelope/',
        'REPORT': 'http://schemas.cordys.com/bsf/uc/webapps/1.0/report'
    };
    var xPaths = {
        "reportInput": "REPORTXML/REPORT:reportinput",
        "reportNode": "/SOAP:Envelope/SOAP:Body/REPORT:RunReport/REPORT:REPORT",
        "reportPath": ".//*[local-name() = 'REPORTPATH']"
    };
    var previewReportXml = "<SOAP:Envelope xmlns:SOAP='http://schemas.xmlsoap.org/soap/envelope/'><SOAP:Body><RunReport xmlns='http://schemas.cordys.com/bsf/uc/webapps/1.0/report'><REPORTTYPE>adhoc</REPORTTYPE><ISMODIFIED>n</ISMODIFIED><REPORT></REPORT></RunReport></SOAP:Body></SOAP:Envelope>";
    var bdiIdPrefix = "bdi";

    function getBusDataIsland(id) {
        var bdiDiv = document.createElement("div");
        bdiDiv.async = true;
        bdiDiv.id = id;
        application.addType(bdiDiv, "wcp.library.data.BusDataIsland");
        document.body.appendChild(bdiDiv);
        return window[id] || bdiDiv;
    }

    /**
     * To get the report request. Uses CPF function runReport in xformscripts.js
     * @param {Object} btnId
     */

    function getReportRequest(btnId) {
        var mapXml = document.getElementById(mapXmlPrefix + btnId);
        var appXml = document.getElementById(appXmlPrefix + btnId);
        mapXml = mapXml.XMLDocument;
        appXml = cordys.cloneXMLDocument(appXml.XMLDocument);
        resolveFormInput(cordys.selectXMLNode(appXml, xPaths.reportInput, reportNamespaces), mapXml);
        var previewReportRequest = cordys.loadXMLDocument(previewReportXml);
        cordys.appendXMLNode(appXml.documentElement, cordys.selectXMLNode(previewReportRequest, xPaths.reportNode, reportNamespaces));
        return previewReportRequest;
    }

    function getResponseHandler(frameId, contentType) {
        var that = this;
        return function(eventObject) {
            var path = cordys.getTextContent(cordys.selectXMLNode(application.event.response, xPaths.reportPath));
            attachDownloader();
            var downloader = document.getElementById("downloader");
            downloader.setFrameId(frameId);
            downloader.openInNewWindow = false;
            downloader.dispositionType = "inline";
            downloader.contentType = contentType;
            downloadFile(path);
            that.responseCallback && that.responseCallback(path);
        };
    }

    function report(btnId, frameId, responseCallback, contentType) {
        this.bdi = getBusDataIsland(bdiIdPrefix + btnId);
        this.btnId = btnId;
        this.reportRequest = cordys.cloneXMLDocument(getReportRequest(btnId));
        this.frameId = frameId;
        this.bdi.onresponse = getResponseHandler.call(this, frameId, contentType);
        this.responseCallback = responseCallback;
    }

    report.prototype = (function() {
        /**
         * To set the input parameters for the report.
         * @param {Object} inputObj - Object with the property names as the parameter names of inputs as defined
         * in iReport and the corresponding parameter values
         */
        var setInput = function(inputObj) {
            for (var i in inputObj) {
                var node = cordys.selectXMLNode(this.reportRequest, ".//*[@parameterName = '" + i + "']");
                cordys.setTextContent(node, inputObj[i]);
            }
        };

        /**
         * To generate the report and display it in the frame mentioned
         */
        var reset = function() {
            this.bdi.clear();
            this.bdi.request = getReportRequest(this.btnId);
            this.bdi.reset();
        };

        /**
         * To show in a new window
         */
        var showInWindow = function(inWindow) {
            var downloader = document.getElementById("downloader");
            downloader.openInNewWindow = inWindow;
        };

        return {
            setInput: setInput,
            reset: reset,
            showInWindow: showInWindow
        };
    })();

    return report;
})();

scriptCallback && scriptCallback("IReport Generation");